package com.logicfishsoftware.wolfgo.meta;

import org.openrdf.repository.RepositoryException;
import org.openrdf.repository.object.ObjectConnection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.logicfishsoftware.wolfgo.api.VirtualFileSystem;
import com.logicfishsoftware.wolfgo.api.VirtualFileSystem.Handler;
import com.logicfishsoftware.wolfgo.api.VirtualFileSystemException;

/**
 * Default handler that does not manage the repository connection.
 *
 */
public class SimpleSemanticFSHandler implements SemanticFileSystemProvider.SemanticFileSystemHandler {
	private final Logger log = LoggerFactory.getLogger(SimpleSemanticFSHandler.class);
	private final ObjectConnection connection;
	private Handler handler;
	
	public SimpleSemanticFSHandler(ObjectConnection connection) {
		this.connection = connection;
	}
	public SimpleSemanticFSHandler(ObjectConnection connection,Handler handler) {
		this(connection);
		this.handler = handler;
	}

	@Override
	public void shutdown(VirtualFileSystem sys) {
		log.info("shutdown.");
		if(handler!=null) {
			handler.shutdown(sys);
		}
	}
	
	@Override
	public ObjectConnection getConnection(
			VirtualFileSystem fileSystem) throws RepositoryException {
		return connection;
	}

	@Override
	public void disposeConnection(ObjectConnection connection) throws RepositoryException {
		log.info("close.");
		connection.commit();
	}

	@Override
	public void rollback(VirtualFileSystemException e,
			ObjectConnection connection) {
		log.error("Rolling back on error.",e);
		try {
			connection.rollback();
		} catch (RepositoryException e1) {
			e1.printStackTrace(System.err);
		}
	}

}
